package stack;

import java.util.Scanner;

/**
 * 数组模拟栈
 */
public class MyStack {
    public static void main(String[] args) {
        //stack.ArrayStack stack = new stack.ArrayStack(10);
        LinkStack stack = new LinkStack(10);
        String key = "";
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        while (loop) {
            System.out.println("Key: show, push, pop, exit");
            key = scanner.next();
            switch (key) {
                case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("输入插入值");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    stack.pop();
                    break;
                case "exit":
                    System.out.println("退出栈");
                    loop = false;
            }
        }
    }
}

/**
 * 数组模拟栈
 */
class ArrayStack {
    private int maxSize;//栈的大小
    private int[] stack;//栈
    private int top = -1;//栈顶。初始化为-1

    //构造器
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    //栈满
    public boolean isFull() {
        return this.top == this.maxSize - 1;
    }

    //栈空
    public boolean isEmpty() {
        return this.top == -1;
    }

    //入栈
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public void pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        System.out.println("出栈数据为：" + value);
    }

    //遍历栈
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\t", i, stack[i]);
        }
        System.out.println();
    }
}

/**
 * 链表模拟栈
 */
class LinkStack {
    private int maxSize;
    private StackNode top;
    private int numNode;

    //构造
    public LinkStack(int maxSize) {
        this.maxSize = maxSize;
        this.top = null;
        this.numNode = 0;
    }

    //栈空
    public boolean isEmpty() {
        return top == null;
    }

    //栈满
    public boolean isFull() {
        return numNode == maxSize;
    }

    //出栈
    public void pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        System.out.println("出栈数据为：" + top.getVal());
        top = top.getNext();
        numNode--;
    }

    //入栈
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        StackNode curNode = new StackNode(value);
        curNode.setNext(top);
        top = curNode;
        numNode++;
    }

    //遍历栈
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        int index = numNode;
        StackNode midNode = top;
        while (midNode != null) {
            System.out.printf("stack[%d]=%d\t", index, midNode.getVal());
            midNode = midNode.getNext();
        }
        System.out.println();
    }
}

class StackNode {
    private int val;
    private StackNode next;

    public StackNode(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public StackNode getNext() {
        return next;
    }

    public void setNext(StackNode next) {
        this.next = next;
    }
}


